/*
 * ReimsCopieSessionBean.java
 *
 * Created on 22 novembre 2007, 11:00
 */
package com.reimscopie.beans.session ;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.entities.* ;


/**
 * This abstract class provide some utility method to access persistence.
 */
public abstract class ReimsCopieSessionBean {
  
  /**
   * Create a new <code>ReimsCopieSessionBean</code> instance.
   */
  protected ReimsCopieSessionBean() {
    
  }
  
  
  /**
   * Find an entity bean in the database.
   *
   * @param manager The persistence manager to use.
   * @param clazz The entity class.
   * @param id The value id.
   *
   * @return The entity bean, or <code>null</code> if the id if not found.
   */
  protected <T> T getEntity(EntityManager manager,Class<T> clazz,long id) {
    
    Long l = new Long(id) ;
    
    T bean = manager.find(clazz,l) ;
    
    return bean ;
    
  }
  
  
  /**
   * Find an entity value in the database.
   *
   * @param manager The persistence manager to use.
   * @param clazz The entity bean class.
   * @param id The value id.
   *
   * @return The entity value, or <code>null</code> if the id is not found.
   */
  protected <T extends Value> T getValue(EntityManager manager,
      Class<? extends ValueBean<T>> clazz,long id) {
    
    ValueBean<T> bean = getEntity(manager,clazz,id) ;
    
    // not found
    if (bean == null)
      return null ;
    
    // transformation
    else
      return bean.toValue() ;
    
  }
  
  
  /**
   * Execute a named query, and return a single result.
   *
   * @param manager The persistence manager to use.
   * @param name The query name.
   * @param params The query parameters, or <code>null</code> if the query
   * has no parameter.
   *
   * @return The query result, or <code>null</code> if the query has no
   * result.
   *
   * @exception NonUniqueResultException If more than one result .
   */
  protected Object executeSingleQuery(EntityManager manager,String name,
      String... params)
  throws NonUniqueResultException {
    
    Query query = manager.createNamedQuery(name) ;
    if (params != null) {
      for (int i=0;i<params.length;i++)
        query.setParameter(i+1,params[i]) ;
    }
    
    try {
      return query.getSingleResult() ;
    }
    catch (NoResultException e) {
      return null ;
    }
    
  }
  
  
  /**
   * Execute a named query, and return a multiple result.
   *
   * @param manager The persistence manager to use.
   * @param name The query name.
   * @param params The query parameters, or <code>null</code> if the query
   * has no parameter.
   *
   * @return The query result, or <code>null</code> if the query has no
   * result.
   */
  protected Object[] executeMultipleQuery(EntityManager manager,String name,
      String... params) {
    
    Query query = manager.createNamedQuery(name) ;
    if (params != null) {
      for (int i=0;i<params.length;i++)
        query.setParameter(i+1,params[i]) ;
    }
    
    try {
      List<?> list = query.getResultList() ;
      Object[] array = new Object[list.size()] ;
      list.toArray(array) ;
      return array ;
    }
    catch (NoResultException e) {
      return null ;
    }
    
  }
  
}
